<?php
namespace app\clubadmin\controller;
use \think\Controller;
use \think\Db;
use \think\File;

class ClubCourse extends Common
{

    public function index(){

    $clubs=Db::name("clubs")->select();
    $this->assign("clubs",$clubs);
    /*return "hello";*/
    //如何访问视图层
    /*$view = new \think\View();
    return $view->fetch('index');*/
    //调用模型层，实现获取用户列表数据
    $clubcourses=Db::name("clubcourses")->select();
    $this->assign("clubcourses",$clubcourses);
    return $this->fetch();
  }



public function jiekou(){
   $rs=db("clubcourses")->select();
   return json($rs);
}



  public function add(){
    $file=request()->file('pic');
    $info="";
    if($file){
       $info = $file->rule('uniqid')->validate(['size'=>3000000,'ext'=>'jpg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'static'.DS.'posters');
      }
      $data=array();
    if (request()->isPost()) {
   
      $data=[
        'cc_id'=>input('param.cc_id'),
        'cc_pic'=>$info->getFilename(),
        'cc_name'=>input('param.cc_name'),
        'cc_intro'=>input('param.cc_intro'),
         'cc_price'=>input('param.cc_price'),
         
      ];
 var_dump($data);
     $validate = \think\Loader::validate('ClubCourse');
if(!$validate->check($data)){
    $this->error($validate->getError());
}
      //存入数据库
    $res=Db::name("clubcourses")->insert($data);
    //处理结果
    if ($res) {
      $this->success("添加课程成功");
    }else{
      $this->error("添加课程失败");
    }
    }

  }

public function update(){
    if (request()->isPost()) {
      $file=request()->file('cc_pic');

      if ($file) {
        $cc_id=input('param.cc_id');
        $picname_DB=Db::name("clubcourses")->where('cc_id',$cc_id)->column('cc_pic');  
        //dump($picname_DB);
        $file_name="C:\wamp\www\\"."sport\public\static\posters\\".$picname_DB[0];
        /*dump($file_name);exit;*/
        /*dump($file_name);
        dump(file_exists($file_name));exit;*/
        /*echo $file_name;
        exit;*/
        if (file_exists($file_name)) {          
          unlink($file_name);
          $info = $file->rule('uniqid')->validate(['size'=>5000000,'ext'=>'jpg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'static'.DS.'posters');

          if ($info) {
            $data=[
              'cc_id'=>input('param.cc_id'),
              'cc_name'=>input('param.cc_name'),
              
              'cc_pic'=>$info->getFilename(),
               'cc_intro'=>input('param.cc_intro'),
              'cc_price'=>input('param.cc_price')
            ];
        
            $res=Db::name("clubcourses")->update($data);    
            if ($res) {
              $this->success("修改成功");

            }else{
              $this->error("修改失败");
            }

          }

        }else $this->error('数据库中无指定图片');
      }else $this->error('未获取图片');
    }else $this->error('未获取数据');
  }

public function delete($id){
    //先删除头像：find方法和unlink
    if (Db::name("clubcourses")->delete($id)) 
      {  
        echo Db::name("clubcourses")->getLastSql();exit;
      $this->success('删除成功');
    }
    else $this->error('删除失败');
  }

  public function edit(){
    $bid=input('cc_id');
    $clubcourses=Db::name("clubcourses")->find($bid);
    echo json_encode($clubcourses);
  }
}
